#!/bin/bash

# devices defined in aws block device mapping are mounted defaultly to /disk0, /disk1...
# this script makes it easy to remount these devices to other directory
# e.x. `remount` /disk0 /tmp will mount the device for /disk0 to /tmp in all nodes


usage() {
 echo "Usage: remount <source directory> <destination directory>"
 echo "assume the backend device of the <source directory> is the same on all nodes"
 exit 1
}

if [[ "$#" != "2" ]]; then
 usage
fi

SRC="$1"
DEST="$2"
SRC_DEVICE=`df | grep "$SRC" | grep '^/dev' | cut -d' ' -f1`
if [[ "$SRC_DEVICE" == "" ]]; then
 echo "$SRC is not mounted separately to any device under /dev"
 exit 1
else
 echo "device for $SRC is $SRC_DEVICE"
fi

for HOST in `cat /vagrant/files/workers`; do
  ssh $HOST /bin/bash <<EOF &
set -e

echo "remounting $SRC to $DEST on $HOST"

sudo mkdir -p $SRC && sudo chown -R `whoami` $SRC
sudo mkdir -p $DEST && sudo chown -R `whoami` $DEST

sudo umount $SRC
sudo mount $SRC_DEVICE $DEST
EOF
done

wait
